<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <div class="h_top">
          <div class="bread_title">
            <span>更新任务</span>
          </div>
          <div class="save_button">
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-back" size="mini" @click="goBack">返回</el-button>
            </div>
            <div class="buttonBox">
              <el-button type="primary" icon="el-icon-document-checked" size="mini" @click="save">保存</el-button>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
    <div>
      <el-row>
        <el-col :span="24">
          <el-form ref="form" :model="data" :inline="true" :rules="formRules">
            <el-input v-model="data.id" type="hidden" />
            <el-row :gutter="10">
              <el-col :span="3">
                <el-form-item prop="coding">
                  <div>编号</div>
                  <el-input v-model="data.coding" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item prop="name">
                  <div>任务名称</div>
                  <el-input v-model="data.name" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="3">
                <el-form-item prop="rank">
                  <div>级别</div>
                  <el-select v-model="data.rank" size="mini">
                    <el-option v-for="(item, index) in theRank" :key="index" :value="index" :label="item" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="7">
                <el-form-item prop="detail">
                  <div>细节</div>
                  <el-input v-model="data.detail" size="mini" />
                </el-form-item>
              </el-col>
              <el-col :span="3">
                <el-form-item prop="status">
                  <div>状态</div>
                  <el-select v-model="data.status" size="mini">
                    <el-option v-for="(item, index) in commonStatus" :key="index" :value="index" :label="item" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="2">
                <el-form-item prop="sort">
                  <div>排序</div>
                  <el-input v-model="data.sort" size="mini" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <el-tabs>
            <el-tab-pane label="适用于">
              <div>
                <el-row :gutter="10">
                  <el-col :span="12">
                    <h3>维护计划</h3>
                    <el-table :data="plan_list">
                      <el-table-column label="编码">
                        <template v-slot="{row}">
                          {{row.coding}}-{{row.name}}
                        </template>
                      </el-table-column>
                      <el-table-column label="资产">
                        <template v-slot="{row}">
                          {{ row.coding }}-{{ row.name }}
                        </template>
                      </el-table-column>
                    </el-table>
                  </el-col>
                  <el-col :span="12">
                    <h3>任务组</h3>
                    <el-table :data="groups">
                      <el-table-column label="任务组号" prop="task_group.coding" />
                      <el-table-column label="名称" prop="task_group.name" />
                    </el-table>
                  </el-col>
                </el-row>
              </div>
            </el-tab-pane>
            <el-tab-pane label="描述">
              <el-row :gutter="10">
                <el-col :span="4">
                  <div>
                    <span>创建时间日期</span>
                    <el-input v-model="data.create_time" placeholder="创建时间日期" class="mtb10" :disabled="true" size="mini" />
                  </div>
                  <div>
                    <span>创建人</span>
                    <el-input v-model="data.cr_admin.name" placeholder="创建人" class="mtb10" :disabled="true" size="mini" />
                  </div>
                  <div v-if="data.update_time">
                    <div>
                      <span>最近更新</span>
                      <el-input v-model="data.update_time" placeholder="最近更新" class="mtb10" :disabled="true" size="mini" />
                    </div>
                    <div>
                      <span>更新者</span>
                      <el-input v-model="data.up_admin.name" placeholder="更新者" class="mtb10" :disabled="true" size="mini" />
                    </div>
                  </div>
                </el-col>
                <el-col :span="20">
                  <Tinymce id="content" v-model="data.content" />
                </el-col>
              </el-row>
            </el-tab-pane>
          </el-tabs>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import API from '@/api/project/task'
import { theRank, commonStatus } from '@/utils/options'
import Tinymce from '@/components/Tinymce/index'
export default {
  inject: ['backUrl', 'delOne', 'update', 'appendOtherAdmin', 'delOtherAdmin', 'getWeightFormat', 'goUrl'],
  components: {
    Tinymce
  },
  data() {
    return {
      list: [],
      data: {
        'cr_admin': {
          'name': ''
        }
      },
      groups: [],
      plans: [],
      plan_list: [],
      theRank,
      commonStatus,
      formRules: {
        coding: [
          {
            required: true,
            message: '请输入编号',
            trigger: 'blur'
          }
        ],
        name: [
          {
            required: true,
            message: '请输入任务名称',
            trigger: 'blur'
          }
        ],
        rank: [
          {
            required: true,
            message: '请选择级别',
            trigger: 'change'
          }
        ],
        status: [
          {
            required: true,
            message: '请选择状态',
            trigger: 'change'
          }
        ],
        sort: [
          {
            required: true,
            message: '请输入排序',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    const id = this.$route.params.id
    API.getOne(id).then(response => {
      if (response.error_code === 63404) {
        this.$alert('您请求的任务不存在', '错误', {
          confirmButtonText: '确定',
          callback: () => {
            this.backUrl()
          }
        })
      }
      this.data = response.data
      this.plan_list = response.data.plan_list
    })
    API.taskOfPlan(id).then(PlanResponse => {
      if (!PlanResponse.data.error_code) {
        this.plans = PlanResponse.data
      }
    })
    API.taskOfGroup(id).then(GroupResponse => {
      if (!GroupResponse.data.error_code) {
        this.groups = GroupResponse.data
      }
    })
  },
  methods: {
    confirmDel() {
      const id = this.data.id
      this.delOne(API, id, true)
    },
    save() {
      const thisData = this.data
      const thisForm = this.$refs.form
      this.update(API, thisData, thisForm)
    },
    goBack() {
      this.goUrl('/project/task')
    }
  }
}
</script>
